package com.bulletelc.apollopre.module.display.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.framework.BaseActivity;
import com.bulletelc.apollopre.module.display.product.resultList.ResultListActivity;
import com.bulletelc.apollopre.utils.SharedPreferencesUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CX on 2018/1/18.
 * 搜索界面
 */

public class SearchActivity extends BaseActivity {

    private SearchView searchView;
    private ListView listView;
    private Button btnClear;

    private List<String> list = new ArrayList<>();
    private HistoryAdapter adapter;

    @Override
    public int getLayoutRes() {
        return R.layout.search_activity;
    }

    @Override
    public void assignViews() {
        searchView = findViewById(R.id.sv_search);
        listView = findViewById(R.id.lv_search_list);
        btnClear = findViewById(R.id.btn_clear);
    }

    @Override
    public void setListener() {
        if (list.size() == 0) {
            btnClear.setVisibility(View.INVISIBLE);
        }
        btnClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearHistory();
                list.clear();
                adapter.notifyDataSetChanged();
                btnClear.setVisibility(View.INVISIBLE);
            }
        });
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                int flag = 0;
                Intent intent = new Intent(getApplicationContext(), ResultListActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_SEARCH_KEYWORD, query);
                if (TextUtils.isEmpty(query)) {
                    Toast.makeText(getApplicationContext(), "请输入查找内容！", Toast.LENGTH_SHORT).show();
                    return true;
                }
                for (String name : list) {
                    if (query.equals(name)) {
                        flag = 1;
                        break;
                    }
                }
                if (flag == 1) {
                    startActivity(intent);
                } else if (flag == 0) {
                    list.add(query);
                    SharedPreferencesUtil.putInt(getApplicationContext(), "recordNum", list.size());
                    SharedPreferencesUtil.putString(getApplicationContext(), "record_" + (list.size() - 1), list.get((list.size() - 1)));
                    startActivity(intent);
                }
                searchView.clearFocus();
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        getHistory();
    }

    /**
     * 获取历史记录
     */
    private void getHistory() {
        int num = SharedPreferencesUtil.getInt(getApplicationContext(), "recordNum");
        for (int i = 0; i < num; i++) {
            list.add(SharedPreferencesUtil.getString(getApplicationContext(), "record_" + i));
        }
        adapter = new HistoryAdapter(this, list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                searchView.setQuery(list.get(i), false);
            }
        });
    }

    /**
     * 清空历史记录
     */
    private void clearHistory() {
        SharedPreferencesUtil.remove(getApplicationContext(), "recordNum");
        for (int i = 0; i < list.size(); i++) {
            SharedPreferencesUtil.remove(getApplicationContext(), "record_" + i);
        }
    }
}
